<template>
  <component
    is="a-modal"
    v-model:visible="visible"
    :width="600"
    :title="title"
    :mask-closable="false"
    :ok-loading="loading"
    @cancel="close"
    @before-ok="submit"
  >
    <!-- 审核信息 -->
    <a-form ref="formRef" :model="formData" :rules="rules" :auto-label-width="true">
      <a-form-item label="审核状态" field="status">
        <sa-radio v-model="formData.status" dict='audit' @change="handleStatusChange" />
      </a-form-item>
      <!-- 拒绝理由输入框 -->
      <a-form-item label="拒绝理由" field="reject_reason" v-if="showRejectReason">
        <a-textarea v-model="formData.reject_reason" placeholder="请输入拒绝理由" />
      </a-form-item>
    </a-form>
  </component>
</template>

<script setup>
import { ref, reactive, computed } from 'vue'
import { Message } from '@arco-design/web-vue'
import api from '../api/reservation'

const emit = defineEmits(['success'])
// 引用定义
const visible = ref(false)
const loading = ref(false)
const formRef = ref()
const mode = ref('')
const showRejectReason = ref(false) // 控制拒绝理由输入框显示

let title = computed(() => {
  return '预约审核' 
})

// 表单信息
const formData = reactive({
 reservation_id: null,
 lang_code: '',
 project_id: '',
 reservation_time_slot: '',
 reservation_person: '',
 id_number: '',
 phone_number: '',
 reservation_number: null,
 reservation_time: '',
 status: '',
  reject_reason: '' // 新增拒绝理由字段
})

// 验证规则
const rules = {
  status: [{ required: true, message: '请选择审核状态', trigger: 'change' }],
  reject_reason: [
    { required: (rule, value, callback) => {
        if (formData.status === 'rejected' && !value) {
          callback(new Error('拒绝理由必需填写'));
        } else {
          callback();
        }
      }, 
      trigger: 'change' 
    }
  ]
}

// 打开弹框
const open = async (type = 'add', reservationId) => {
  mode.value = type
  formData.reservation_id = reservationId
  visible.value = true
  formRef.value.resetFields()
   
  // 初始化时重置状态和拒绝理由
  // formData.status = ''
  // formData.reject_reason = ''
  // showRejectReason.value = false
  // console.log(formData) 
  // handleStatusChange(formData.status)
}
// const initPage = async () => {
// 	console.log(formData.status)
// 	handleStatusChange(formData.status)
// }

// 设置数据
const setFormData = async (data) => {
  for (const key in formData) {
    if (data[key] !== null && data[key] !== undefined) {
      formData[key] = data[key]
    }
  }
  handleStatusChange(data['status'])
 // console.log(data['status'])
 
}

// 处理状态变化
const handleStatusChange = (value) => {
	
 	
  showRejectReason.value = value == 2
  if (!showRejectReason.value) {
    formData.reject_reason = ''
  }
}

// 提交审核
// 数据保存
const submit = async (done) => {
	 
  // const validate = await formRef.value?.validate()
  // if (!validate) {
    loading.value = true
    let data = { ...formData }
    let result = {}   
      // 修改数据
    result = await api.update(data.reservation_id, data)

    if (result.code === 200) {
      Message.success(data.status == 1 ? '审核通过' : '已拒绝')
      emit('success')
      done(true)
    }
    // 防止连续点击提交
    setTimeout(() => {
      loading.value = false
    }, 500)
  // }
  done(false)
}

// 关闭弹窗
const close = () => (visible.value = false)


defineExpose({ open, setFormData })
</script>